package cn.bon.common.interceptor;

import cn.bon.common.tool.UserContext;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author Lc_cL
 * @Date 2023/12/15
 * @Description
 */

@Component
public class UserInfoInterceptor implements HandlerInterceptor {
    public UserInfoInterceptor(){
        System.out.println("123");
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取token信息
        String tk = request.getHeader("tk");

        //2.如果获取到，存入threadLocal
        if (StrUtil.isNotBlank(tk)) {
            UserContext.setUser(tk);
        }
        //放行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserContext.removeUser();
    }
}
